2020-2021 Sunseeker Telemetry and Lighting System
adc10_a_ex2_intRef.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
66 //******************************************************************************
67 #include "driverlib.h"
68 
69 #define TIMER_A_PERIOD 80
70 
71 void main (void)
72 {
73  //Stop Watchdog Timer
74  WDT_A_hold(WDT_A_BASE);
75 
76  //Set P1.0 to output direction
77  GPIO_setAsOutputPin(
78  GPIO_PORT_P1,
79  GPIO_PIN0
80  );
81 
82  //Initialize the ADC10_A Module
83  /*
84  * Base Address for the ADC10_A Module
85  * Use internal ADC10_A bit as sample/hold signal to start conversion
86  * USE MODOSC 5MHZ Digital Oscillator as clock source
87  * Use default clock divider of 1
88  */
89  ADC10_A_init(ADC10_A_BASE,
90  ADC10_A_SAMPLEHOLDSOURCE_SC,
91  ADC10_A_CLOCKSOURCE_ADC10OSC,
92  ADC10_A_CLOCKDIVIDER_1);
93 
94  ADC10_A_enable(ADC10_A_BASE);
95 
96  /*
97  * Base Address for the ADC10_A Module
98  * Sample/hold for 16 clock cycles
99  * Do not enable Multiple Sampling
100  */
101  ADC10_A_setupSamplingTimer(ADC10_A_BASE,
102  ADC10_A_CYCLEHOLD_16_CYCLES,
103  ADC10_A_MULTIPLESAMPLESDISABLE);
104 
105  //Configure Memory Buffer
106  /*
107  * Base Address for the ADC10_A Module
108  * Use input A0
109  * Use positive reference of Internally generated Vref
110  * Use negative reference of AVss
111  */
112  ADC10_A_configureMemory(ADC10_A_BASE,
113  ADC10_A_INPUT_A0,
114  ADC10_A_VREFPOS_INT,
115  ADC10_A_VREFNEG_AVSS);
116 
117  //Enable Memory Buffer interrupt
118  ADC10_A_clearInterrupt(ADC10_A_BASE,
119  ADC10IFG0);
120  ADC10_A_enableInterrupt(ADC10_A_BASE,
121  ADC10IE0);
122 
123  //Configure internal reference
124  //If ref generator busy, WAIT
125  while ( REF_BUSY == Ref_isRefGenBusy(REF_BASE) ) ;
126  //Select internal ref = 1.5V
127  Ref_setReferenceVoltage(REF_BASE,
128  REF_VREF1_5V);
129  //Internal Reference ON
130  Ref_enableReferenceVoltage(REF_BASE);
131 
132  //Configure TA0 to provide delay for reference settling ~75us
133  //Start timer in Up Mode with Capture compare interrupt enabled
134  Timer_A_initUpModeParam initUpParam = {0};
135  initUpParam.clockSource = TIMER_A_CLOCKSOURCE_SMCLK;
136  initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
137  initUpParam.timerPeriod = TIMER_A_PERIOD;
138  initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
139  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
140  TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
141  initUpParam.timerClear = TIMER_A_SKIP_CLEAR;
142  initUpParam.startTimer = true;
143  Timer_A_initUpMode(TIMER_A1_BASE, &initUpParam);
144 
145 
146  //LPM0, TA0_ISR will force exit
147  __bis_SR_register(CPUOFF + GIE);
148 
149  //Disable timer Interrupt
150  Timer_A_disableCaptureCompareInterrupt(TIMER_A0_BASE,
151  TIMER_A_CAPTURECOMPARE_REGISTER_0);
152 
153  for (;;)
154  {
155  //Delay between conversions
156  __delay_cycles(5000);
157 
158  //Enable and Start the conversion
159  //in Single-Channel, Single Conversion Mode
160  ADC10_A_startConversion(ADC10_A_BASE,
161  ADC10_A_SINGLECHANNEL);
162 
163  //LPM0, ADC10_A_ISR will force exit
164  __bis_SR_register(CPUOFF + GIE);
165  //For debug only
166  __no_operation();
167 
168 
169  }
170 }
171 
172 // ADC10_A interrupt service routine
173 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
174 #pragma vector=ADC10_VECTOR
175 __interrupt
176 #elif defined(__GNUC__)
177 __attribute__((interrupt(ADC10_VECTOR)))
178 #endif
179 void ADC10_A_ISR (void) {
180 
181  switch (__even_in_range(ADC10IV,12)){
182  case 0: break; //No interrupt
183  case 2: break; //conversion result overflow
184  case 4: break; //conversion time overflow
185  case 6: break; //ADC10HI
186  case 8: break; //ADC10LO
187  case 10: break; //ADC10IN
188  case 12: //ADC10IFG0
189 
190  //Automatically clears ADC10IFG0 by reading memory buffer
191  //ADC10MEM = A0 > 0.5V?
192  if (ADC10_A_getResults(ADC10_A_BASE) < 0x155){
193  //Clear P1.0 LED off
195  GPIO_PORT_P1,
196  GPIO_PIN0
197  );
198  } else {
199  //Set P1.0 LED on
201  GPIO_PORT_P1,
202  GPIO_PIN0
203  );
204  }
205 
206  //Clear CPUOFF bit from 0(SR)
207  //Breakpoint here and watch ADC_Result
209  break;
210  default: break;
211  }
212 }
213 
214 // TIMER_A interrupt service routine
215 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
216 __interrupt
217 #elif defined(__GNUC__)
218 __attribute__((interrupt(TIMER0_A0_VECTOR)))
219 #endif
220 void TA0_ISR (void) {
221 
222  Timer_A_stop(TIMER_A0_BASE);
223  //Exit LPM0 on return
224  LPM0_EXIT;
225 }
void ADC10_A_ISR(void)
void main(void)
void TA0_ISR(void)
#define TIMER_A_PERIOD
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
__delay_cycles(500000)